package com.qfedu.openplateform.manager.controller;


import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiMapping;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.ApiMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/api_mapping")
public class ApiMappingController {

    @Autowired
    private ApiMappingService apiMappingService;

    // 查询客户列表信息

    @RequestMapping("/table")
    public TableData<ApiMapping> getAllApiMapping(ApiMapping apiMapping, int page, int limit){
        List<ApiMapping> apiMappingList = apiMappingService.getAllApiMapping(apiMapping, page, limit);
        PageInfo pageInfo = new PageInfo(apiMappingList);

        TableData<ApiMapping> tableData = new TableData<>();

        tableData.setCode(0);
        tableData.setCount(pageInfo.getTotal());
        tableData.setData(apiMappingList);
        tableData.setMsg("查询所有路由信息成功");

        return tableData;
    }

    // 更新路由信息
    @RequestMapping("/update")
    public AjaxMessage update(ApiMapping apiMapping){
        try{
            apiMappingService.updateApiMapping(apiMapping);
            return new AjaxMessage(true,"修改对应客户信息成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new AjaxMessage(false,"修改对应客户信息异常");
    }

    // 添加客户信息
    @RequestMapping("/add")
    public AjaxMessage add(ApiMapping apiMapping){

        try{
            apiMappingService.addApiMapping(apiMapping);
            return new AjaxMessage(true,"添加客户信息成功！");

        }catch (Exception e){
            e.printStackTrace();
        }
        return new AjaxMessage(false,"添加客户信息失败");
    }

    // 根据ID删除客户信息
    @RequestMapping("/del")
    public AjaxMessage delete(int[] ids) {
//        try{
//            customerService.deleteCustomer(int[] ids);
//            return new AjaxMessage(true,"删除客户信息成功");
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return new AjaxMessage(false,"删除客户信息失败");
//    }
        for (int id : ids) {
            apiMappingService.deleteApiMapping(ids);
            return new AjaxMessage(true, "删除成功");
        }
        return new AjaxMessage(false, "删除失败");
    }
}
